package com.neogb.shareringtone;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ListSound extends ListActivity {
	
	private static final int MENU_STOP = Menu.FIRST;
	private static final int CONTEXTMENU_SEND_MAIL = Menu.FIRST;
	private RingtoneManager mRingtoneManager;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mRingtoneManager = new RingtoneManager(this);
        final Bundle extras = getIntent().getExtras();
        String type = extras.getString("type");
        if (type.equals("ALARM"))
        	mRingtoneManager.setType(RingtoneManager.TYPE_ALARM);
        else if (type.equals("NOTIFICATION"))
        	mRingtoneManager.setType(RingtoneManager.TYPE_NOTIFICATION);
        else if (type.equals("RINGTONE"))
        	mRingtoneManager.setType(RingtoneManager.TYPE_RINGTONE);
        else
        	mRingtoneManager.setType(RingtoneManager.TYPE_ALL);
        mRingtoneManager.setStopPreviousRingtone(true);
        
        Cursor cursor = mRingtoneManager.getCursor();
        
        startManagingCursor(cursor);

        ListAdapter adapter = new SimpleCursorAdapter(this,
        							android.R.layout.simple_list_item_1,
        							cursor,
        							new String[] {cursor.getColumnName(RingtoneManager.TITLE_COLUMN_INDEX)},
        							new int[] {android.R.id.text1});
        setListAdapter(adapter);
        
        registerForContextMenu(getListView());
        
    }
    
    @Override
    public void onDestroy() {
    	super.onDestroy();
    	mRingtoneManager.stopPreviousRingtone();
    }
    
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	mRingtoneManager.getRingtone(position).play();
    }
    
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, CONTEXTMENU_SEND_MAIL, 0, getString(R.string.contextmenu_send_mail));
	}

    
    public boolean onContextItemSelected(MenuItem item) {
		final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch (item.getItemId()) {
			case CONTEXTMENU_SEND_MAIL:
				final Intent sendIntent = new Intent(Intent.ACTION_SEND);
		    	sendIntent.setType("audio/x-ogg");
		    	sendIntent.putExtra(Intent.EXTRA_STREAM, mRingtoneManager.getRingtoneUri(info.position));
		    	sendIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.mail_subject));
		    	sendIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.mail_text));
		    	startActivity(Intent.createChooser(sendIntent, "Title:"));
				return true;
			default:
				return super.onContextItemSelected(item);
		}
		
	}
    
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_STOP, 0, getString(R.string.menu_stop));
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_STOP:
        	mRingtoneManager.stopPreviousRingtone();
            return true;
        }
        return false;
    }

    
}
